package com.sk.server;

import com.sk.constant.Constants;
import com.sk.mapper.SUserMapper;
import com.sk.pojo.RespBean;
import com.sk.utils.FdfsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserListServer {

    @Autowired
    SUserMapper sUserMapper;


    @Autowired
    FdfsUtils fastDFSUtil;

    String ip = Constants.IP;

    //UserListController
    public RespBean like(String tel, String username, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String role = request.getSession().getAttribute("role").toString();
        map.put("tel", "%" + tel + "%");
        map.put("username", "%" + username + "%");
        if ("子管理员".equals(role)) {
            map.put("role", "%普通用户%");
            return RespBean.success("查询成功", sUserMapper.likeByTelAndUsername(map));
        }

        if ("超级管理员".equals(role)) {
            map.put("role", "%%");
            return RespBean.success("查询成功", sUserMapper.likeByTelAndUsername(map));
        }

        return RespBean.error("查询失败");
    }

    public RespBean delete(int id,int rid) {

        //超级管理员不能删除自己,也可防止注入(安全)
        if(rid == sUserMapper.getRoleIdById(id)){
            return RespBean.error("删除失败");
        }

        int i = sUserMapper.deleteById(id);
        if (i > 0) {
            return RespBean.success("删除成功");
        } else {
            return RespBean.error("删除失败");
        }
    }


    public RespBean update(Integer id, String username, String tel, String pwd,
                           MultipartFile file, Integer sex, Integer age,
                           String toRole, int rid) {

        int i;
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("sex",sex);
        map.put("age", age);
        map.put("tel",tel);
        if(username.length()>0){
            map.put("username", username);
        }
        if(pwd.length()>0){
            map.put("pwd", pwd);
        }

        if (file!=null) {
            String path = fastDFSUtil.upload(file);
            map.put("imgUrl", ip + path);
        }

        //登录用户为管理员
        if (rid == 2) {

            //当管理员修改自己时,也可防止注入(安全)
            if(2==sUserMapper.getRoleIdById(id)){
                if(toRole.length()>0){
                    return RespBean.error("不能修改自己的用户");
                }
            }

            i = sUserMapper.updateAdmin(map);
            if (i>0) {
                return RespBean.success("修改成功");
            } else {
                return RespBean.error("修改失败");
            }

        }

        //登录用户为超级用户
        if (rid == 1) {

            //当超级管理员修改自己时,也可防止注入(安全)
            if(1==sUserMapper.getRoleIdById(id)){
                if(!toRole.isEmpty()){
                    return RespBean.error("不能修改自己的用户");
                }
            }


            if ("子管理员".equals(toRole)){
                map.put("rid",2);
            }


            if ("普通用户".equals(toRole)){
                map.put("rid",3);
            }

            i = sUserMapper.updateAdmin(map);

            if (i>0) {
                return RespBean.success("修改成功");
            } else {
                return RespBean.error("修改失败");
            }

        }

        return RespBean.error("修改失败");

    }


    public RespBean insert (String username, String tel,String pwd,
                            MultipartFile file,
                            Integer sex, Integer age, String toRole,
                            HttpServletRequest request){
        Integer rid = (Integer) request.getSession().getAttribute("rid");
        Map<String, Object> uMap = new HashMap<>();
        Map<String, Object> urMap = new HashMap<>();
        uMap.put("sex",sex);
        uMap.put("age", age);
        uMap.put("tel",tel);

        if(username.length()>0){
            uMap.put("username", username);
        }

        if(pwd.length()>0){
            uMap.put("pwd", pwd);
        }

        if (file!=null) {
            String path = fastDFSUtil.upload(file);
            uMap.put("imgUrl", ip + path);
        }

        int i_u = sUserMapper.addUser(uMap);

        if("普通用户".equals(toRole)){
            urMap.put("uid",uMap.get("id"));
            urMap.put("roleId",getRidByRname(toRole));
        }

        if("子管理员".equals(toRole)){
            if(rid==2){
                return RespBean.error("您没有权限,请联系超级管理员");
            }

            if(rid==1){
                urMap.put("uid",uMap.get("id"));
                urMap.put("roleId",getRidByRname(toRole));
            }
        }

        if("超级管理员".equals(toRole)){
            return RespBean.error("您没有权限,请联系超级运维人员");
        }

        int i_ur = sUserMapper.addRoleId(urMap);

        if (i_ur >0 && i_u>0){
           return RespBean.success("添加成功");
        }else{
           return RespBean.success("添加失败");
        }
    }


    int getRidByRname(String rname){
        if("超级管理员".equals(rname)){
            return 1;
        }
        if("子管理员".equals(rname)){
            return 2;
        }

        if("普通用户".equals(rname)){
            return 3;
        }
        return 0;
    }


}
